﻿package{
	import flash.display.*;
	import flash.geom.Point;
	public class stroke{
		private var coordinateArray:Array;//坐标
		private var l:int;//坐标长度
		private var strk:Array;//最终识别的得到的字符串
		public var zb:Array;//起始点及拐点坐标
		private var cd:int;//有效段个数
		private var dc:Array ;//分段方向
		private var direct:String;//最终方向------------------------
		
		private var oo:int;//坏段个数
		private var haha:Array ;//得到的可用于计算斜率的有效点集合
		private var zb2:Array=new Array();
		private var nn:Array=new Array();
		public function stroke(a:Array){//构造函数
			this.coordinateArray=new Array();
			this.l=0;
			this.strk=new Array();
			this.zb=new Array();
			this.cd=0;
			this.direct="";
			this.oo=0;
			this.dc=new Array(); 
			this.haha=new Array();
			
			this.init(a);
			//this.rcgz();
			}
		private function init(ar:Array):void{
				//trace(ar.join(";"));
				this.coordinateArray=ar;//初始化坐标组
				this.l=ar.length;			
				if(ar.join("")!=""){//如果不为空则调用识别函数
					this.rcgz();					
					}
			}
		private function getSlope():Array{
			var slope:Array =new Array();
				var k:int =0;
				var s:int = 0;
				var ux:int=0;//x坐标
				var uy:int=0;//y坐标
			for(k=0;k<this.l;k++){
				if(coordinateArray[k]!=""){
					if(k==0){
						var ii:Array = coordinateArray[0].split(",");
						ux= int(ii[0]);//x坐标
						uy = int(ii[1]);//y坐标
						this.haha.push(coordinateArray[0]);//存放起始点
						}
					else {
						
						var iii:Array = coordinateArray[k].split(",");//
						
						var uux:int = int(iii[0]);
						var uuy:int = int(iii[1]);
						if((Math.abs (uux-ux)+Math.abs (uuy-uy))>=4){//横坐标和纵坐标绝对值差和大于4像素，可调------------
							slope.push((uy-uuy)/(ux-uux));//存放每一段的斜率值
							this.haha.push(coordinateArray[k]);//存放每一个结束点
							ux=uux;
							uy=uuy;
							}
						
						}
					}
					
					
			}
			this.haha.push(coordinateArray[this.coordinateArray.length -1]);//存放结束点
			return slope;
			}
		private function cmpp(a:Number,b:Number):int{
				if(a>=3.5&&b>=3.5)
					return 2;//竖斜，斜率可调--------------
				else if ((a>=0.3&&a<3.5)&&(b>=0.3&&b<3.5))
				{
					return 4;//捺
				}
				else if ((a>=-0.3&&a<0.3)&&(b>=-0.3&&b<0.3))
				{
					return 1;//横
				}
				else if((a>=-3.5&&a<-0.3)&&(b>=-3.5&&b<-0.3))
					return 3;//撇
				else if(a<-3.5&&b<-3.5)
					return 2;//竖
				else if((a<-3.5&&b>=3.5)||(a>=3.5&&b<-3.5))
					return 2;//竖
				else 
					return -1;
					
					//return -1;
			
			}
		public function cmp(a:Number,b:Number,c:Number):int{
				var ok:Array =[a,b,c];
				var sum:int = 0;//记录返回值
				var f:int=0;
				
				if(cmpp(ok[0],ok[1])!=-1){//3段中有2段为同一段则可接受，否则不能接受。
					return cmpp(ok[0],ok[1]);//
					}
				else if(cmpp(ok[0],ok[2])!=-1){
					
					return cmpp(ok[0],ok[2]);//
					}
				else if(cmpp(ok[1],ok[2])!=-1){
					return cmpp(ok[1],ok[2]);//
					}
				else 
					return -1;
				/*
				sum =cmpp(ok[0],ok[1])+cmpp(ok[0],ok[2])+cmpp(ok[1],ok[2]);
							
					
				sum=sum-1;
				if(cmpp(ok[0],ok[1]))
					return 2;
				else if((sum%1000)>=200)
					return 4;
				else if((sum%100)>=20)
					return 1;
				else if((sum%10)>=2)
					return 3;
				else 
					return -1;
					*/
					
			}
		private function fdbj(a:Array):Array {
				var res:Array = new Array();//记录每一段是什么
				var q:int = 2;//
				//var n:int = 0;
				for(;q<a.length;q=q+3){
						res.push(cmp(a[q-2],a[q-1],a[q]));//3个为一段，每一段调用cmp来识别是什么
					}
					
				return res;
			}
		public function ig(zz:Array):Array {
			var hello:Array=new Array();//除错后的结果
				
				var a:Array =new Array();//abcd分别用来记录横竖撇捺的出现的位置
				var b:Array =new Array();
				var c:Array =new Array();
				var d:Array =new Array();
				//var i:int =0;
				
				
				
				//var theend:Array = new Array();
				//trace(zz.join("%"));
				for(var i:int=0;i<zz.length;i++){
						if(zz[i]!=-1)
							hello.push(zz[i]);
						else{
							hello.push(0);
							this.oo=this.oo+1;
						}
						
					}
					//trace(hello.join (";"));
					this.cd=hello.length ;
				if(hello.length >=4){
					for(var y:int=0;y<hello.length;y++){
						
						if(hello[y]==1){
							a.push (y);
							//trace(a.join (")"));
							if(a.length>=3)
							{
								if(b.length >0&&b.length <3)
								{
									for(i=0;i<b.length ;i++){
										hello[b[i]]=0;
						
										}
									}
									if(c.length >0&&c.length <3)
								{
									for(i=0;i<c.length ;i++){
										hello[c[i]]=0;
										
										}
									}
									if(d.length >0&&d.length <3)
								{
									for(i=0;i<d.length ;i++){
										hello[d[i]]=0;
										
										}
									}
									b.length =0;
									c.length =0;
									d.length =0;
									
								}
							}
						if(hello[y]==2){
							b.push (y);
							if(b.length>=3)
							{
								if(a.length >0&&a.length <3)
								{
									for(i=0;i<a.length ;i++){
										hello[a[i]]=0;
										
										}
									}
									if(c.length >0&&c.length <3)
								{
									for(i=0;i<c.length ;i++){
										hello[c[i]]=0;
										
										}
									}
									if(d.length >0&&d.length <3)
								{
									for(i=0;i<d.length ;i++){
										hello[d[i]]=0;
										
										}
									}
									a.length =0;
									c.length =0;
									d.length =0;
									
								}
							}
						if(hello[y]==3){
							c.push (y);
							if(c.length>=3)
							{
								if(a.length >0&&a.length <3)
								{
									for(i=0;i<a.length ;i++){
										hello[a[i]]=0;
										
										}
									}
									if(b.length >0&&b.length <3)
								{
									for(i=0;i<b.length ;i++){
										hello[b[i]]=0;
										
										}
									}
									if(d.length >0&&d.length <3)
								{
									for(i=0;i<d.length ;i++){
										hello[d[i]]=0;
										
										}
									}
									b.length =0;
									a.length =0;
									d.length =0
									
								}
							}
						if(hello[y]==4){
							d.push (y);
							if(d.length>=3)
							{
								if(a.length >0&&a.length <3)
								{
									for(i=0;i<a.length ;i++){
										hello[a[i]]=0;
										
										}
									}
									if(b.length >0&&b.length <3)
								{
									for( i=0;i<b.length ;i++){
										hello[b[i]]=0;
										
										}
									}
									if(c.length >0&&c.length <3)
								{
									for(i=0;i<c.length ;i++){
										hello[c[i]]=0;
										
										}
									}
									b.length =0;
									c.length =0;
									a.length =0;
									
								}
							}
					}
					
						
				}
				/*if(a.length <3||b.length <3){
					if(b.length <3){
						for(var p:int=0;p<b.length ;p++){
							hello[b[p]]=0;
							}
						}
						if(a.length <3){
						for(p=0;p<a.length ;p++){
							hello[a[p]]=0;
							}
						}
					}
					*/
				//trace (hello.join ("#"));
				a.splice (0);
				b.splice (0);
				c.splice(0);
				d.splice(0);
				//trace(cd);
				return hello;
			}
		private function c(a:Array ):Array {
			var co:int=0;
			var q:int=0;
			var h:int=0;
			var i:int=0;
			if(a[0]==0){
				for(i=0;i<a.length ;i++){
					if(a[i]!=0){
						q=a[i];
						break;
					}
						
					}
				}
			else q=a[0];
			//trace (q);
			for(i=0;i<a.length ;i++){
				
				
				if(a[i]==0){
					co=co+1;
					q=a[i+1];
					}
				if(a[i]!=0){
					
					
					if(a[i]!=0&&co!=0){
					h=a[i];
					//trace(co);
					for(var v:int=0;v<=co/2;v++){
						a[i-co+v]=q;
						a[i-v]=h;
						}
						q=h;
						co=0;
					}
				}
				}
				for(i=0;i<a.length ;i++){
					if(a[i]==0)
						a[i]=a[i+1];
					}
				//trace(a.join ("&"));
				//trace(a.length+"//"+this.haha.length);
				var b=this.haha[0].split(",");
				var bx:int=int(b[0]);
				var by:int=int(b[1]);
				var ex:int;
				var ey:int;
				var d:Array=new Array();
				//var nn:Array=new Array();
				if(cd<2){
					var e:Array=this.haha[haha.length-1].split(",");
						ex=int(e[0]);
						ey=int(e[1]);
						if(ex<bx&&ey>by){
							this.nn.push(7);
							d.push (1);
							}
						else if(ex>bx&&ey>by){
							this.nn.push(8);
							d.push(1);
							}							
						}
				else{
				for(i=0;i<a.length;i++){
					e=this.haha[i*3+3].split(",");
					ex=int(e[0]);
					ey=int(e[1]);
					if(a[i]=="1"){
							this.nn.push(1);
							if(ex>bx){								
								d.push(1);
							}
							else{
								//trace(ex+";;"+ey+"/"+bx+";;"+by);
								d.push(0);
								}							
						}
						else if(a[i]=="2"){
							this.nn.push(2);
							if(ey>by)
								d.push(1);
							else 
								d.push(0);
								
							}
						else if(a[i]=="3"){
							if(ex<bx&&ey>by){
								this.nn.push(3);
								d.push(1);
								}
							else if(ex>bx&&ey<by){
								this.nn.push(5);
								d.push(1);
								}
							}
						else if(a[i]=="4"){
							if(ex>bx&&ey>by){
								
								this.nn.push(4);
								d.push(1);
								}
							else if(ex<bx&&ey<by){
								this.nn.push(6);
								d.push(1);
								}
							}
							bx=ex;
							by=ey;
					}
				}
				for( i=0;i<d.length;i++){
					if(i==0){
						this.dc.push(d[0]);
						}
					
					else{
						if(d[i]!=d[i-1]){
							
							this.dc.push(d[i]);
							
							}
						}
					}
				
				//trace(this.nn.join(""));
				//trace(this.dc.join(""));
				this.direct=this.dc.join("");
			return this.nn;
			}
		private function getRe(a:Array):Array {
				//var s:String ="";
				a=c(a);
				//trace(a.join ("{}"));
				//trace(this.haha.join("；"));
				var tt:int =0;
				var we:int =0;
				var theend:Array =new Array();
				//var zb:Array =new Array();
				for(var i:int=1;i<a.length ;i++){
					if(a[i]!=a[i-1]){
						tt=tt+1;
						}
					}
				for(i=0;i<a.length;i++){
					if(i==0){
						theend.push(a[0]);
						this.zb.push(this.haha[0]);
						}
					
					else{
						if(a[i]!=a[i-1]){
							we=we+1;
							theend.push(a[i]);
							//this.zb.push(this.haha[3*i+int(this.oo/tt)*we+1]);
							this.zb.push(this.haha[3*i]);
							//trace(this.zb.join("--"));
							}
						}
					}
				this.zb.push(this.haha[this.haha.length-1]);				
				//trace(this.haha.join(";;"));
				return theend;
			}
		private function dir(a:Array ):String {
				var s:String ="";
				
				/*var b=this.zb[0].split(",");
				var bx:int=int(b[0]);
				var by:int=int(b[1]);
				//trace(zb.join("//"));
				var ex:int=0;
				var ey:int=0;
				var nn:Array = new Array();
				var d:Array = new Array();
				var drct:int =0;
				var e:Array = new Array();
				for(var i:int=0;i<zb.length-1;i++){
					e=zb[i+1].split(",");
					ex=int(e[0]);
					ey=int(e[1]);
					if(cd<2){
						if(ex<bx&&ey>by){
							nn.push(7);
							d.push (1);
							}
						else if(ex>bx&&ey>by){
							nn.push(8);
							d.push(1);
							}							
						}
					else{
						//trace(a[i]);
						//trace(bx+"/"+by+"/"+ex+"/"+ey);
						if(a[i]=="1"){
							nn.push(1);
							if(ex>bx){								
								d.push(1);
							}
							else{
								d.push(0);
								}							
						}
						else if(a[i]=="2"){
							nn.push(2);
							if(ey>by)
								d.push(1);
							else 
								d.push(0);
								
							}
						else if(a[i]=="3"){
							if(ex<bx&&ey>by){
								nn.push(3);
								d.push(1);
								}
							else if(ex>bx&&ey<by){
								nn.push(5);
								d.push(1);
								}
							}
						else if(a[i]=="4"){
							if(ex>bx&&ey>by){
								
								nn.push(4);
								d.push(1);
								}
							else if(ex<bx&&ey<by){
								nn.push(6);
								d.push(1);
								}
							}
							bx=ex;
							by=ey;
				}
				}
				for(i=0;i<d.length;i++){
					if(i==0)
						drct=d[0];
					else{
						if(d[i]!=d[i-1])
							drct=0;
						else{
							drct=-1;
							}
						}
					}
					this.dc=d;
					//trace(dc.join ("-"));*/
				//this.direct=drct;
				s=a.join ("");
				//trace(s);
				//nn.splice (0);
				//d.splice (0);
				//e.splice (0);
				return s;
			
			
		}
			
		public function recStroke(s:String):Array {
				
				var ret:Array =new Array();
			trace(this.dc.join("")+"dc");
			
			var strokeCode:StrokeCode=new StrokeCode(s,this.dc.join(""));
				ret=strokeCode.getStrokeCode();
				return ret;
			} 
		public function getStroke():Array {
				return this.strk;
			}
		public function getSE():Array{
				var startAndEndPoint:Array=new Array();
				var sp:Array=new Array();
				var ep:Array=new Array();
				sp=this.zb[0].split(",");
				ep=this.zb[zb.length-1].split(",");
				startAndEndPoint.push(new Point(sp[0],sp[1]));
				startAndEndPoint.push(new Point(ep[0],ep[1]));
				return startAndEndPoint;
				
			}
		/*public function getFillDirection():Array{
			return this.fillDirection;
			}*/
		private function rcgz():void{
			try{
				var slope:Array =new Array();
				slope=getSlope();//获得存放斜率的数组
				//trace(slope.join ("/"));
				//trace(this.haha.join ("////"));
				var cc:Array =new Array();
				cc=fdbj(slope);//分段识别每一段是什么
				//trace(cc.join(";"));
				var td:Array =new Array();
				td=ig(cc);//忽略其中无用的坏段
				//trace(td.join("@"));
				var re:Array = new Array();
				re=getRe(td);//获得最终识别结果
				trace("result"+re.join("$"));
				var st:String = dir(re);
				//trace(st);
				this.strk = recStroke(st);//映射到28笔的基本笔画
				//slope.splice (0);
				//cc.splice (0);
				//td.splice(0);
				//re.splice(0);
				
				
				//this.strk="123";
				}catch(errObject:Error){
				trace(errObject.message);
				}
			}	
		
	}
	
}